package com.zzz.schedule.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zzz.schedule.common.Result;
import com.zzz.schedule.common.ResultCodeEnum;
import com.zzz.schedule.pojo.SysUser;
import com.zzz.schedule.service.SysScheduleService;
import com.zzz.schedule.service.SysUserService;
import com.zzz.schedule.service.impl.SysScheduleServiceImpl;
import com.zzz.schedule.service.impl.SysUserServiceImpl;
import com.zzz.schedule.util.MD5Util;
import com.zzz.schedule.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/*
 * @author zzr
 * @date: 2025/07/14  21:33
 * @description:
 */
@WebServlet("/user/*")
public class SysUserController extends BaseController {

    private SysUserService userService = new SysUserServiceImpl();

    /**
     * 注册时，接收要注册的用户名，校验用户名是否被占用的业务接口
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void checkUsernameUsed(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 接收用户名
        String username = req.getParameter("username");
        // 调用服务处层业务处理方法查询用户名是否已经有对应的用户
        SysUser sysUser = userService.finByUsername(username);
        Result result = Result.ok(null);
        if (null != sysUser) {
            result = Result.build(null, ResultCodeEnum.USERNAME_USED);
        }
        WebUtil.writeJson(resp, result);
    }

    /**
     * 接收用户注册请求的业务处理接口（此接口非 java 中 interface）
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接收客户端提交的json数据，并转换为 User 对象，获取信息
        SysUser resgistUser = WebUtil.readJson(req, SysUser.class);

        // 2. 调用服务层方法，完成注册功能
        // 将参数传入一个 SysUser 对象中
        int rows = userService.regist(resgistUser);

        // 3. 根据注册结果（成功 / 失败）实现页面跳转
         Result result = Result.ok(null);
         if (rows < 1) {
             result = Result.build(null,ResultCodeEnum.USERNAME_USED);
         }
         WebUtil.writeJson(resp, result);
    }

    /**
     * 接收用于登录的请求，完成登录业务的接口
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接收用户名和密码
        SysUser sysUser = WebUtil.readJson(req, SysUser.class);

        // 2. 调用服务层方法，根据用户名查询用户信息
        SysUser loginUser = userService.finByUsername(sysUser.getUsername());
        Result result = null;
        // 3. 判断密码是否匹配
        if (null == loginUser) {
            result = Result.build(null,ResultCodeEnum.USERNAME_ERROR);
        } else if (!MD5Util.encrypt(sysUser.getUserPwd()).equals(loginUser.getUserPwd())) {
            // 密码不匹配
            result = Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        } else {
            // 登录成功之后，将用户的 uid 和 username 相应给客户端
            Map data = new HashMap();
            loginUser.setUserPwd("");//将密码置为空，防止泄露
            data.put("loginUser",loginUser);
            result = Result.ok(data);
        }
        // 将登录结果相应给客户端
        WebUtil.writeJson(resp,result);
    }
}
